package com.dtguai.app.service.impl;


import com.dtguai.app.base.form.BaseFrom;
import com.dtguai.app.base.service.impl.BaseServiceImpl;
import com.dtguai.app.dao.UserDao;
import com.dtguai.app.model.User;
import com.dtguai.app.service.UserService;
import com.dtguai.cache.annotation.Cache;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 用户表
 *
 * @author guo
 * @date 2019-02-15 16:12:24
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    /**
     * 根据条件查询  自定义xml
     */
    @Override
    public List<User> findUserByXml(BaseFrom user) {
        return ((UserDao) getMapper()).selectByUserModel(user);
    }

    /**
     * Select标签 根据主键查询对象
     */
    @Override
    public User findUserById(Integer id) {
        return ((UserDao) getMapper()).findUserById(id);
    }

    /**
     * 根据id从cache读取数据
     * cache  expire 是分钟
     */
    @Override
    @Cache(key = "user:cache:id:{1}", expire = 1)
    public User getUserCacheById(Integer id) {
        return Optional.ofNullable(id).map(x -> selectByKey(id)).orElse(null);
    }

    @Override
    public User login(String mobile, String password) {
        return ((UserDao) getMapper()).login(mobile, password);
    }

    @Override
    public User selectUserByMobile(String mobile) {
        return ((UserDao) getMapper()).selectUserByMobile(mobile);
    }
}